package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.DrawingManage;
import com.hgxd.entity.vo.pagevo.DrawingManagePageVo;
import com.hgxd.result.Result;


import com.hgxd.service.IDrawingManageService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.hgxd.result.ResultCodeEnum.ARGUMENT_VALID_ERROR;

/**
 * <p>
 * 图纸管理 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "图纸管理控制")
@RestController
@RequestMapping("/api/drawing-manage")
@CrossOrigin
public class DrawingManageController {
    @Resource(name = "DrawingManageServiceImpl")
    private IDrawingManageService drawingManageService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增图纸", notes = "新增图纸")
    @ApiImplicitParam(name = "drawingManage", value = "图纸实体类")
    @OperationLogAnnotation(operModule = "图纸管理-新增", operType = "新增", operContent = "新增图纸")
    public Result<String> insert(@RequestBody DrawingManage drawingManage) {
        boolean save = drawingManageService.save(drawingManage);
        return save ? Result.ok("新增成功") : Result.fail("新增失败");
    }

    /**
     * 根据图片类型查询图片
     */
    @PostMapping("/getDrawingByType")
    @ApiOperation(value = "根据图片类型查询图片", notes = "根据图片类型查询图片")
    @ApiImplicitParam(name = "type", value = "图纸类型")
    @OperationLogAnnotation(operModule = "图纸管理-根据图片类型查询图片", operType = "查询", operContent = "根据图片类型查询图片")
    public Result<List<DrawingManage>> getDrawingByType(@RequestParam String type) {
        return Result.ok(drawingManageService.getDrawingByType(type));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新图纸", notes = "更新图纸")
    @ApiImplicitParam(name = "drawingManage", value = "图纸实体类")
    @OperationLogAnnotation(operModule = "图纸管理-更新", operType = "更新", operContent = "更新图纸")
    public Result<String> update(@RequestBody DrawingManage drawingManage) {
        boolean data = drawingManageService.updateById(drawingManage);
        return data ? Result.ok("更新成功") : Result.fail("更新失败");
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除图纸", notes = "批量删除图纸")
    @ApiImplicitParam(name = "ids", value = "id的list集合")
    @OperationLogAnnotation(operModule = "图纸管理-批量删除", operType = "删除", operContent = "批量删除图纸")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(drawingManageService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部图纸", notes = "查询全部图纸")
    @OperationLogAnnotation(operModule = "图纸管理-查询全部", operType = "查询全部", operContent = "查询全部图纸")
    public Result<List<DrawingManage>> findAll() {
        return Result.ok(drawingManageService.list());
    }


    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询一个图纸", notes = "根据id查询一个图纸")
    @ApiImplicitParam(name = "id", value = "图纸的id")
    @OperationLogAnnotation(operModule = "图纸管理-根据id查询一个", operType = "根据id查询一个", operContent = "根据id查询一个图纸")
    public Result<DrawingManage> findOne(@PathVariable Integer id) {
        return Result.ok(drawingManageService.getById(id));
    }

    /**
     * 分页模糊查询 根据图纸类型，图片名称，组织名称模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "分页模糊查询")
    @ApiImplicitParam(name = "drawingManagePageVo", value = "分页参数")
    @OperationLogAnnotation(operModule = "图纸管理-分页模糊查询", operType = "分页模糊查询", operContent = "分页模糊查询图纸")
    public Result<Page<DrawingManage>> findPage(@RequestBody DrawingManagePageVo drawingManagePageVo) {
        LambdaQueryWrapper<DrawingManage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!"".equals(drawingManagePageVo.getOrganizationName()), DrawingManage::getOrganizationName, drawingManagePageVo.getOrganizationName());
        lambdaQueryWrapper.eq(!"".equals(drawingManagePageVo.getType()), DrawingManage::getType, drawingManagePageVo.getType());
        lambdaQueryWrapper.eq(!"".equals(drawingManagePageVo.getName()), DrawingManage::getName, drawingManagePageVo.getName());
        lambdaQueryWrapper.eq(DrawingManage::getIsdelete, "0");
        return Result.ok(drawingManageService.page(new Page<>(drawingManagePageVo.getPageNum(), drawingManagePageVo.getPageSize()), lambdaQueryWrapper));
    }

    /**
     * 上传图片
     */
    @PostMapping("upload")
    @ApiOperation(value = "上传图片", notes = "上传图片")
    @ApiImplicitParam(name = "file", value = "文件")
    @OperationLogAnnotation(operModule = "图纸管理-上传图片", operType = "上传图片", operContent = "上传图片")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return Result.fail("传参为空", ARGUMENT_VALID_ERROR);
        } else {
            return drawingManageService.upload(file);
        }
    }

    /**
     * 下载图片
     */
    @PostMapping("download")
    @ApiOperation(value = "下载图片", notes = "下载图片")
    @ApiImplicitParam(name = "urlString", value = "页面穿过来的文件名字")
    @OperationLogAnnotation(operModule = "图纸管理-下载图片", operType = "下载图片", operContent = "下载图片")
    public Result<String> download(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam String urlString) throws IOException {
        if (StringUtil.isNullOrEmpty(urlString)) {
            return Result.fail("传参为空", ARGUMENT_VALID_ERROR);
        }
        return drawingManageService.download(request, response, urlString);
    }
}

